---
title: Create index
description: A create index operation creates a new index on a set of columns.
---

## Structure

<YamlJsonTabs>
```yaml
create_index:
  table: name of table on which to define the index
  name: index name
  columns:
    column_name:
      collate: collation name
      sort: ASC | DESC
      nulls: FIRST | LAST
      opclass:
        name: operator_class_name
        params:
          - param1=val
          - param2=val
  predicate: conditional expression for defining a partial index
  storage_parameters: comma-separated list of storage parameters
  unique: true | false
  method: btree
```
```json
{
  "create_index": {
    "table": "name of table on which to define the index",
    "name": "index name",
    "columns": [
      "column_name": {
        "collate": "collation name",
        "sort": "ASC | DESC",
        "nulls": "FIRST | LAST",
        "opclass": {
          "name": "operator_class_name",
          "params": [
             "param1=val",
             "param2=val"
          ]
        }
      }
    ]
    "predicate": "conditional expression for defining a partial index",
    "storage_parameters": "comma-separated list of storage parameters",
    "unique": true | false,
    "method": "btree"
  }
}
```
</YamlJsonTabs>

* The field `method` can be `btree`, `hash`, `gist`, `spgist`, `gin`, `brin`.
* You can also specify storage parameters for the index in `storage_parameters`.
* To create a unique index set `unique` to `true`.

## Examples

### Create a `btree` index

Create a `btree` index on the `name` column in the `fruits` table:

<ExampleSnippet example="10_create_index.yaml" languange="yaml" />

### Create a partial index

Create a partial index on the `id` column in the `fruits` table:

<ExampleSnippet example="37_create_partial_index.yaml" languange="yaml" />

### Create an index with storage parameters

Set storage parameters and index method:

<ExampleSnippet
  example="38_create_hash_index_with_fillfactor.yaml"
  languange="yaml"
/>

### Create a unique index

Create a unique index:

<ExampleSnippet example="42_create_unique_index.yaml" languange="yaml" />

### Create an index with custom operator class

Create an index with a custom operator class:

<ExampleSnippet example="54_create_index_with_opclass.yaml" languange="yaml" />
